package com.legoo.studaychineseonline.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.google.gson.Gson;
import com.legoo.studaychineseonline.R;
import com.legoo.studaychineseonline.activity.LoginActivity;
import com.legoo.studaychineseonline.base.BaseFragment;
import com.legoo.studaychineseonline.bean.RegisterBean;
import com.legoo.studaychineseonline.http.API;
import com.legoo.studaychineseonline.http.XUtil;
import com.legoo.studaychineseonline.utils.EncryptionUtil;
import com.legoo.studaychineseonline.utils.RegularUtil;

import org.xutils.common.Callback;

import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;


/**

 */
public class RegisterFragment extends BaseFragment implements View.OnFocusChangeListener {
    Unbinder unbinder;
    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.et_password)
    EditText etPassword;
    @BindView(R.id.et_email)
    EditText etEmail;
    @BindView(R.id.ll_tiaokuan)
    LinearLayout llTiaokuan;
    @BindView(R.id.bt_register)
    Button btRegister;
    Unbinder unbinder1;
    private String sha;
    @Override
    public void onFocusChange(View v, boolean hasFocus) {

    }


    @Override
    protected void initView(View view, Bundle savedInstanceState) {
        unbinder = ButterKnife.bind(this, view);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_register;
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    private void register(final String username, String password, String email, String client_type)  {
        Map<String, Object> map = new HashMap<>();
        map.put("username", username);
        map.put("password", password);
        map.put("email", email);
        map.put("client_type", client_type);
        XUtil.Post(API.REGISTER, map, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {

                RegisterBean registerBean = new Gson().fromJson(result, RegisterBean.class);
                if (registerBean != null && registerBean.getCode() == API.HTTP_OK) {
                    LoginActivity activity = (LoginActivity) getActivity();
                    activity.clickLogin(username);
                }
                showToast(registerBean.getMessage());
                hidePro();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                hidePro();
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }


    @OnClick({R.id.ll_tiaokuan, R.id.bt_register})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_tiaokuan:
                break;
            case R.id.bt_register:
                String userName = etName.getText().toString().trim();
               if (!RegularUtil.isTelUserName(userName)){
                showToast("请输入4-18位英文或数字的用户名");
                   return;
               }
               if (  etPassword.getText().toString().trim().length()<4&&etPassword.getText().toString().trim().length()>20){
                   showToast("请输入6-20位密码");
                   return;
               }

                String email = etEmail.getText().toString().trim();
               if (!RegularUtil.isEmail(email)){
                   showToast("请输入正确格式的邮箱");
                   return;
               }
                try {
                    sha = EncryptionUtil.getSHA(etPassword.getText().toString().trim());
                } catch (NoSuchAlgorithmException e) {
                    e.printStackTrace();
                }
                if ("".equals(sha) || sha == null) {
                    showToast("请重新输入密码");
                    return;
                }
                    showPro();
                register(userName,sha,email,"student");
                break;
                default:
        }
    }
}
